﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;

namespace PNProject.Controllers
{
    public partial class ArticlesController : Controller
    {
        public ActionResult Index(string p)
        {
            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            ViewData["lang"] = lang;
            int page = PNLibraries.PageVerify(p);
            var list = StaticData.newsRepo.GetSome(a => a.Lang == lang && a.CType == 1).OrderByDescending(a=>a.CreateDate).ToList();
            var model = list.Skip((page - 1) * 10).Take(10).ToList();
            ViewData["news"] = model;
            var count = list.Count;
            int mod = count % 10;
            int pc = mod == 0 ? count / 10 : (count / 10) + 1;
            ViewData["page-pc"] = pc;
            ViewData["page-total"] = count;
            ViewData["page-active"] = page;
            ViewData["page-list"] = PNLibraries.GetShowedPage(count, page, 10, 5);
            return View();
        }

        public ActionResult Detail(string id)
        {
            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            ViewData["lang"] = lang;
            var dbo = StaticData.newsRepo.GetOne(a => a.TLink == id && a.Lang == lang);
            if (dbo == null)
                return RedirectToAction("Error", "Home");
            ViewData["news"] = dbo;
            var list = StaticData.newsRepo.GetSome(a => a.Lang == lang && a.TLink != id && a.CType == 1).ToList();
            ViewData["relative"] = list;
            var avatar = StaticData.xuserRepo.GetOne(a => a.Username == dbo.CreateBy).Avatar;
            ViewData["avatar"] = avatar;
            return View();
        }

        public ActionResult Search(string query, string p)
        {
            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            ViewData["lang"] = lang;
            int page = PNLibraries.PageVerify(p);
            var list = StaticData.newsRepo.GetSome(a => a.Lang == lang && a.CType == 1 && a.Title.Contains(query)).ToList();
            var model = list.Skip((page - 1) * 10).Take(10).ToList();
            ViewData["news"] = model;
            var count = list.Count;
            int mod = count % 8;
            int pc = mod == 0 ? count / 10 : (count / 10) + 1;
            ViewData["query"] = query;
            ViewData["page-pc"] = pc;
            ViewData["page-total"] = count;
            ViewData["page-active"] = page;
            ViewData["page-list"] = PNLibraries.GetShowedPage(count, page, 10, 5);
            return View();
        }

    }
}
